﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using OpenLS.Spreadsheet.Internals;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    class ConditionalFormattingRuleViewModel
    {
        private readonly ConditionalFormattingRule _model;

        public ConditionalFormattingRuleViewModel(ConditionalFormattingRule model)
        {
            _model = model;
            _selectedItem = null;
        }
        private static RgbColor GetColor(string text)
        {
            return new RgbColor((Color)ColorConverter.ConvertFromString(text));
        }

        public IEnumerable<object> Items
        {
            get
            {
                yield return new NamedDifferentialStyleRecord(UIStringTable.LightRedFillWithDarkRedText, new DifferentialStyleRecord
                {
                    Font = new Font( f => f.Foreground = GetColor("#FF9C0006") ),
                    Background = new PatternFill(GetColor("#FFFFC7CE"))
                });
                yield return new NamedDifferentialStyleRecord(UIStringTable.YellowFillWithDarkYellowText, new DifferentialStyleRecord
                {
                    Font = new Font (f => f.Foreground = GetColor("#FF9C6500") ),
                    Background = PatternFill.NewBackgroundSolidFill(GetColor("#FFFFEB9C"))
                });
                yield return new NamedDifferentialStyleRecord(UIStringTable.GreenFillWithDarkGreenText, new DifferentialStyleRecord
                {
                    Font = new Font (f => f.Foreground = GetColor("#FF006100") ),
                    Background = PatternFill.NewBackgroundSolidFill(GetColor("#FFC6EFCE"))
                });
                yield return new NamedDifferentialStyleRecord(UIStringTable.LightRedFill, new DifferentialStyleRecord
                {
                    Background = PatternFill.NewBackgroundSolidFill(GetColor("#FFFFC7CE"))
                });
                yield return new NamedDifferentialStyleRecord(UIStringTable.RedText, new DifferentialStyleRecord
                {
                    Font = new Font ( f => f.Foreground = GetColor("#FF9C0006") ),
                });
                yield return new NamedDifferentialStyleRecord(UIStringTable.RedBorder, new DifferentialStyleRecord
                {
                    Borders = new Borders(new Border { Color = GetColor("#FF9C0006"), Style = BorderStyle.Medium })
                });
                yield return new Message(UIStringTable.CustomFormat_, EditRuleStyleCommand);

            }
        }


        private object _selectedItem;
        public object SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value;

            var rule = (StandardConditionalFormattingRule)Model;
            object o = value;
            if (o is Message)
            {
               EditRuleStyle(null, rule);
            }
            else
            {

                rule.DifferentialStyleRecord = ((NamedDifferentialStyleRecord)o).Record;
            }
            }
        }
        public ICommand EditRuleStyleCommand
        {
            get
            {
                return new DelegateCommand((o) => EditRuleStyle(o, (StandardConditionalFormattingRule) Model));
            }
        }

        private BackgroundFormatter _BackgroundFormatter;
        public BackgroundFormatter BackgroundFormatter
        {
            get
            {
                if (_BackgroundFormatter != null)
                    return _BackgroundFormatter;
                StandardConditionalFormattingRule rule = Model as StandardConditionalFormattingRule;
                if (rule == null)
                    return null;
                _BackgroundFormatter =  new BackgroundFormatter(rule.DifferentialStyleRecord);
                return _BackgroundFormatter;
            }
        }

        private FontFormatter _FontFormatter;
        public FontFormatter FontFormatter
        {
            get
            {
                if (_FontFormatter != null)
                    return _FontFormatter;
                StandardConditionalFormattingRule rule = Model as StandardConditionalFormattingRule;
                if (rule == null)
                    return null;
                _FontFormatter =  new FontFormatter(rule.DifferentialStyleRecord);
                return _FontFormatter;
            }
        }

        public ConditionalFormattingRule Model
        {
            get { return _model; }
        }

        private void EditRuleStyle(object sender, StandardConditionalFormattingRule rule)
        {
            var old = rule.DifferentialStyleRecord;
            if (false)
                rule.DifferentialStyleRecord = rule.DifferentialStyleRecord.Clone();
            else
                rule.DifferentialStyleRecord = new DifferentialStyleRecord();
            var vm = new ConditionalFormattingRuleViewModel(rule);
            if (PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement, UIStringTable.FormatCells, vm, new[] { new FormattingRuleStylePropertiesResources() }))
            {
                vm.Apply(rule);
            }
            else
            {
                rule.DifferentialStyleRecord = old;
            }
        }

        private void Apply(StandardConditionalFormattingRule rule)
        {
            if (this.FontFormatter != null)
                FontFormatter.ApplyRule(rule);
            if (this.BackgroundFormatter != null)
                this.BackgroundFormatter.ApplyRule(rule);
        }
    }
}